﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace QuickExcel
{
    public static class Angle
    {
        public static readonly decimal Degree = 1M;
        public static readonly decimal Grad = 0.9000009M;
        public static readonly decimal Minute = 0.0166666666666667M;
        public static readonly decimal Radian = 57.2957795130823M;
        public static readonly decimal Revolution = 360M;
        public static readonly decimal Second = 0.000277777777777778M;
    }

    public static class Memory
    {
        public static readonly decimal Bit = 1M;
        public static readonly decimal Kilobit = 1024M;
        public static readonly decimal Megabit = 1048576M;
        public static readonly decimal Gigabit = 1073741824M;
        public static readonly decimal Terabit = 1099511627776M;
        public static readonly decimal Petabit = 1125899906842620M;
        public static readonly decimal Exabit = 1152921504606850000M;
        public static readonly decimal BYTE = 8M;
        public static readonly decimal KILOBYTE = 8192M;
        public static readonly decimal MEGABYTE = 8388608M;
        public static readonly decimal GIGABYTE = 8589934592M;
        public static readonly decimal TERABYTE = 8796093022208M;
        public static readonly decimal PETABYTE = 9007199254740990M;
        public static readonly decimal EXABYTE = 9223372036854780000M;
    }

    public static class Length
    {
        public static readonly decimal Foot = 1M;
        public static readonly decimal Inch = 0.0833333333333333M;
        public static readonly decimal Meter = 3.28083989501312M;
        public static readonly decimal NauticalMile = 6076.1154855643M;
        public static readonly decimal StatuteMile = 5280M;
        public static readonly decimal Yard = 3M;
    }

    public static class Energy
    {
        public static readonly decimal BTU = 1M;
        public static readonly decimal CalorieIT = 0.00396832071932779M;
        public static readonly decimal CalorieTh_mic = 0.00396566683139091M;
        public static readonly decimal ElectronVolt = 1.5184978085729E-22M;
        public static readonly decimal Erg = 9.47817120313316E-11M;
        public static readonly decimal FootPound = 0.00128506746341696M;
        public static readonly decimal HorsepowerHour = 2544.4335768757M;
        public static readonly decimal Joule = 0.000947817120313316M;
        public static readonly decimal WattHour = 3.41214163312793M;
    }

    public static class Force
    {
        public static readonly decimal Dyne = 1M;
        public static readonly decimal Newton = 100000M;
        public static readonly decimal PoundForce = 444822.2M;
    }

    public static class Liquid
    {
        public static readonly decimal Cup = 1M;
        public static readonly decimal Gallon = 16M;
        public static readonly decimal GallonImp = 19.21521M;
        public static readonly decimal Liter = 4.22583333333333M;
        public static readonly decimal Ounce = 0.125M;
        public static readonly decimal OunceImp = 0.120095M;
        public static readonly decimal Pint = 2M;
        public static readonly decimal PintImp = 2.401899M;
        public static readonly decimal Quart = 4M;
        public static readonly decimal QuartImp = 4.8038M;
        public static readonly decimal Tablespoon = 0.0625M;
        public static readonly decimal Teaspoon = 0.0208333333333333M;
    }

    public static class Mass
    {
        public static readonly decimal Caret = 1M;
        public static readonly decimal Grain = 0.3239945M;
        public static readonly decimal Gram = 5M;
        public static readonly decimal OunceAvdp = 141.7476M;
        public static readonly decimal OunceTroy = 155.5174M;
        public static readonly decimal PoundAvdp = 2267.962M;
        public static readonly decimal PoundTroy = 1866.209M;
        public static readonly decimal Stone = 31751.47M;
        public static readonly decimal Ton = 4535924M;
    }

    public static class Surface
    {
        public static readonly decimal Acre = 1M;
        public static readonly decimal Hectare = 2.47105381467165M;
        public static readonly decimal SqFoot = 2.29568411386593E-05M;
        public static readonly decimal SqInch = 1.59422507907356E-07M;
        public static readonly decimal SqMeter = 0.000247105381M;
        public static readonly decimal SqMiles = 639.999999999998M;
        public static readonly decimal SqYard = 0.000206611570383505M;
    }

    public static class Time
    {
        public static readonly decimal Day = 1M;
        public static readonly decimal Hour = 0.0416666666666667M;
        public static readonly decimal LeapYear = 366M;
        public static readonly decimal Minute = 0.000694444444444444M;
        public static readonly decimal Second = 1.15740740740741E-05M;
        public static readonly decimal Year = 365M;
    }

    public static class Volume
    {
        public static readonly decimal CubicFoot = 1M;
        public static readonly decimal CubicInch = 0.000578703703703703M;
        public static readonly decimal CubicMeter = 35.3146667214885M;
        public static readonly decimal CubicYard = 27M;
    }

    public static class Units
    {
        public static void ConvertUnit(string sCategory,string sFrom, object[,] arrFromValue, string sTo)
        {
            decimal fromValue,mFromValue;            
            
            for (int i=1; i<=arrFromValue.GetLength(0);i++)
            {
                for (int j = 1; j <= arrFromValue.GetLength(1); j++)
                {
                    fromValue = (decimal)arrFromValue[i,j];

                    if (sCategory.Equals("Temperature"))
                    {
                        switch (sFrom)
                        {
                            case "Fahrenheit": fromValue = (fromValue - 32) * (5 / 9);
                                break;
                            case "Kelvin": fromValue = fromValue - 273;
                                break;
                        }

                        switch (sTo)
                        {
                            case "Fahrenheit":  arrFromValue[i,j] = fromValue * 9 / 5 - 32;
                                break;
                            case "Kelvin":  arrFromValue[i,j] = fromValue + 273;
                                break;
                            default:  arrFromValue[i,j] = fromValue; ;
                                break;
                        }
                    }
                    else
                    {
                        decimal mToValue;

                        Type CategoryType = Type.GetType("QuickExcel." + sCategory);
                        mFromValue = (decimal)CategoryType.GetField(sFrom).GetValue(CategoryType);
                        mToValue = (decimal)CategoryType.GetField(sTo).GetValue(CategoryType);

                         arrFromValue[i, j] = fromValue * mFromValue / mToValue;
                    }
                }
            }            
        }
    }
}
